<!DOCTYPE html>
<html>
</head>
  <meta charset="utf-8" />
  <title>document.write</title>
</head>
<body>
  <p>在当前窗口中使用document.write()</p>
  <form>
    <p>
      <input type="button" id="rewritePage" value="replace the text" />
    </p>

  <script type="text/javascript">
  //事件绑定
  function addEvent(elem,evtType,func) {
    if(elem.addEventListener) {
      elem.addEventListener(evtType,func,false);
    }else if (elem.attachEvent) {
      elem.attachEvent("on"+evtType,func);
    }else{
      elem["on"+evtType]=func;
    }
    }

  //脚本正文
  //replace the page with new markup
  function reWrite() {
    //assemnle content for new window
    var newContent = '<!DOCTYPE html>';
    newContent += '<html>';
    newContent += '<head>';
    newContent += '<meta charset="utf-8">';
    newContent += '<title>A new doc</title>';
    newContent += '</head>';
    newContent += '<body>';
    newContent += '<h1>this document is brand new!</h1>';
    newContent += '<p>click the back button to see the original document.</p>';
    newContent += '</body>';
    newContent += '</html>';
    //write html to new window
    document.write(newContent);
    document.close();//close layout stream
    }
    //apply behaviors when document has loaded
    function init(){
      //do this only if this browser can handle DOM methods
      if(document.getElementById){
        //point to the button
        var oButtonRewrite = document.getElementById('rewritePage');
        //if it exists ...
        if(oButtonRewrite){
          //apply event handler
          addEvent(oButtonRewrite,'click',reWrite);
        }
      }
    }
    addEvent(window,'load',init);
    </script>
  </body>
  </html>
